<template>
  <div class="zc-table-common">
    <div class="zc-table-event-btn">
      <el-button type="primary" icon="el-icon-plus" @click.native="eventTab('新增', 'second')">新增信息分类</el-button>
    </div>
    <div class="zc-table-body">
      <el-table
        :data="sortTable"
        :empty-text="emptyText"
        row-key="id"
        v-loading="tableLoading"
        :tree-props="{children: 'data', hasChildren: 'hasChildren'}"
        border
        style="width: 100%">
        <el-table-column
          width="300"
          prop="sortName"
          label="分类名称">
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="isActivity"
          label="状态">
          <template slot-scope="scope">
            <span>
              <i class="el-icon-success diy-success" v-if="scope.row.isActivity"></i>
              <i class="el-icon-error diy-error" v-else></i>
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="metaKeywords"
          label="网页META关键词">
        </el-table-column>
         <el-table-column
          prop="metaDescription"
          label="网页META描述">
        </el-table-column>
        <el-table-column
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native="eventTab('编辑', 'second', scope.row)">编辑</el-button>
            <template v-if="!scope.row.isActivity">
              <el-button type="text" size="small" @click.native="toDeletePublishSort(scope.row.id)">删除</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { GetPublishSortTable, DeletePublishSort } from '@/api/requestConfig'
  export default {
    name: 'InformationSortTable',
    created() {
      // 获取表格
      this.getInformationSortTable()
    },
    computed: {
      ...mapGetters([
        'commonTabInfo'
      ])
    },
    watch: {
      'commonTabInfo.text': function(val, oldVal) {
        if (val === 'refresh' && this.commonTabInfo.custom === 'informationSort') {
          this.clearSearch()
        } else if (val === 'update' && this.commonTabInfo.custom === 'informationSort') {
          this.searchTable()
        }
      }
    },
    data() {
      return {
        emptyText: '未匹配到信息分类数据',
        sortTable: [],
        pageInfo: {
          PageIndex: 1,
          PageSize: 10
        },
        tableLoading: true
      }
    },
    methods: {
      // tabs操作
      eventTab(text, tabsName, custom) {
        this.$store.dispatch('handleTabInfo', {
          text: text,
          tabsName: tabsName,
          custom: custom
        })
      },
      // 清空搜索条件
      clearSearch() {
        this.getInformationSortTable()
      },
      // 搜索
      searchTable() {
        this.getInformationSortTable()
      },
      // 获取信息分类表格
      getInformationSortTable() {
        this.tableLoading = true
        GetPublishSortTable().then(res => {
          if (res.data) {
            this.sortTable = res.data
          } else {
            this.$message.error(res.message)
          }
          this.tableLoading = false
        })
      },
      // 删除信息分类
      toDeletePublishSort(code) {
        var self = this
        this.$confirm('是否删除此信息分类?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
          console.log(code)
          DeletePublishSort(code).then(res => {
            if (res.code === 200) {
              self.$message({
                type: 'success',
                message: '删除成功!'
              })
              self.searchTable()
            } else {
              self.$message.error(res.message)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/zc-table-common.scss";
</style>
